package vn.edu.saigontech.ncms.web.student;

import java.util.Collection;

import vn.edu.saigontech.ncms.model.enumeration.Currency;
import vn.edu.saigontech.ncms.model.result.PlacementTestType;
import vn.edu.saigontech.ncms.model.system.Money;
import vn.edu.saigontech.ncms.service.PlacementTestTypeService;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class PlacementTestTypeAction extends BaseAction {
	
	private Long id;
	private String name;
	private String description;
	private Double fee;

	public String showAdd() {
		return SUCCESS;
	}
	
	public String add() {
		PlacementTestType type = new PlacementTestType();
		type.setName(this.name);
		type.setDescription(this.description);
		type.setFee(new Money(this.fee, Currency.VND));
		type = PlacementTestTypeService.INSTANCE.persist(type);
		
		this.addLoggingDetail(BaseAction.ADD, "logging.placementTestType.add", type.getName());
		return SUCCESS;
	}
	
	public String showEdit() {
		PlacementTestType type = PlacementTestTypeService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("type", type);
		return SUCCESS;
	}
	
	public String edit() {
		PlacementTestType type = PlacementTestTypeService.INSTANCE.findById(this.id);
		type.setName(this.name);
		type.setDescription(this.description);
		type.setFee(new Money(this.fee, Currency.VND));
		type = PlacementTestTypeService.INSTANCE.persist(type);
		
		this.addLoggingDetail(BaseAction.ADD, "logging.placementTestType.edit", type.getName());
		return SUCCESS;
	}
	
	public String list() {
		Collection<PlacementTestType> types = PlacementTestTypeService.INSTANCE.findAll();		
		REQUEST().setAttribute("types", types);
		return SUCCESS;
	}
	
	public String delete() {
		PlacementTestType type = PlacementTestTypeService.INSTANCE.findById(this.id);
		if(PlacementTestTypeService.INSTANCE.deleteById(this.id)) {
			this.addLoggingDetail(BaseAction.DELETE, "logging.placementTestType.delete", type.getName());
			return SUCCESS;
		}
		this.setKeyMessage("confirm.deleteUnsuccessfully", "placementTestType");
		return ERROR;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Double getFee() {
		return fee;
	}

	public void setFee(Double fee) {
		this.fee = fee;
	}
}